﻿/*
 * @author: wizardc
 */

using System;
using System.Collections.Generic;
using System.IO;
using Dou.Utils;
using DouEditor.Utils;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;

namespace DouEditor.Resource
{
    /// <summary>
    /// AB 包打包窗口
    /// </summary>
    public class AssetBundleBuildWindow : EditorWindow
    {
        [MenuItem("DouFramework/资源/构建AssetBundle", false, 11000)]
        public static void ShowWindow()
        {
            var window = EditorWindow.GetWindow<AssetBundleBuildWindow>(false, "构建AssetBundle");
            window.minSize = new Vector2(1024, 768);
        }

        private PlatformType _nowPlatform;

        private VisualElement _root;
        private ListView _ruleFileList;
        
        private Toggle _isEditorUsed;
        private ObjectField _sceneObj;
        private TextField _txtDesc;
        private TextField _outputFolder;
        private Button _btnOutputFolder;
        private TextField _copyFolder;
        private Button _btnCopyFolder;
        private TextField _contrastFolder;
        private Button _btnContrastFolder;
        private TextField _incrementOutputFolder;
        private Button _btnIncrementOutputFolder;
        private TextField _packageOutputFolder;
        private Button _btnPackageOutputFolder;
        private TextField _mainVersion;
        private IntegerField _subVersion;
        private IntegerField _hotFixedVersion;
        private Toggle _clearFolder;
        private Toggle _copyToStreamingAssets;
        private Toggle _encryptABName;
        private Toggle _encryptAB;
        private Toggle _isDev;
        private Toggle _isSymbol;
        private Toggle _openDebugger;
        private EnumFlagsField _logLevel;
        
        private ListView _ruleList;
        
        private Toggle _togActive;
        private TextField _descName;
        private EnumField _rulePackerType;
        private Toggle _togOverrideBundleName;
        private TextField _customBundleName;
        private TextField _assetSearchPath;
        private Button _btnSelectFolder;
        private EnumField _assetSearchOption;
        private TextField _assetSearchPattern;
        private TextField _assetSearchRegex;
        private EnumField _manifestInfoType;

        private BuildSetting _buildSetting;
        private List<string> _ruleFilePathList = new List<string>();
        private int _ruleFileSelectIndex = -1;
        private BuildRule _buildRule;

        private List<BuildRuleItem> _rules;
        private int _rulesSelectIndex = -1;
        private BuildRuleItem _buildRuleItem;
        
        public void CreateGUI()
        {
            _root = rootVisualElement;
            
            var template = EditorUtil.LoadWindowUXML<AssetBundleBuildWindow>();
            template.CloneTree(_root);
            
            // 监听项目文件资源改变
            EditorApplication.projectChanged -= OnProjectChanged;
            EditorApplication.projectChanged += OnProjectChanged;
            
            _buildSetting = BuildSetting.LoadSetting();
            
            var buildBtn = _root.Q<Button>("BtnBuildForce");
            buildBtn.clicked += async () =>
            {
                if (_buildRule != null)
                {
                    if (_nowPlatform == PlatformType.Nonsupport)
                    {
                        EditorUtility.DisplayDialog("提示", "当前平台不支持打包！", "确定");
                        return;
                    }
                    if (EditorUtility.DisplayDialog("提示", $"确定打包 - \"{_buildRule.desc}\"？", "确定", "取消"))
                    {
                        if (await AssetBundleBuildUtil.Build(_nowPlatform, _buildRule, false))
                        {
                            EditorUtility.DisplayDialog("提示", "打包完毕！", "确定");
                        }
                        else
                        {
                            EditorUtility.DisplayDialog("提示", "打包失败！", "确定");
                        }
                    }
                }
            };
            var increBtn = _root.Q<Button>("BtnBuildIncre");
            increBtn.clicked += async () =>
            {
                if (_buildRule != null)
                {
                    if (_nowPlatform == PlatformType.Nonsupport)
                    {
                        EditorUtility.DisplayDialog("提示", "当前平台不支持打包！", "确定");
                        return;
                    }
                    if (EditorUtility.DisplayDialog("提示", $"确定打增量包 - \"{_buildRule.desc}\"？", "确定", "取消"))
                    {
                        if (await AssetBundleBuildUtil.Build(_nowPlatform, _buildRule, false))
                        {
                            EditorUtility.DisplayDialog("提示", "打包完毕！", "确定");
                        }
                        else
                        {
                            EditorUtility.DisplayDialog("提示", "打包失败！", "确定");
                        }
                    }
                }
            };
            var packageBtn = _root.Q<Button>("BtnBuildInstallationPackage");
            packageBtn.clicked += async () =>
            {
                if (_buildRule != null)
                {
                    if (_nowPlatform == PlatformType.Nonsupport)
                    {
                        EditorUtility.DisplayDialog("提示", "当前平台不支持打包！", "确定");
                        return;
                    }
                    if (EditorUtility.DisplayDialog("提示", $"确定打安装包 - \"{_buildRule.desc}\"？", "确定", "取消"))
                    {
                        if (await AssetBundleBuildUtil.BuildInstallationPackage(_nowPlatform, _buildRule))
                        {
                            EditorUtility.DisplayDialog("提示", "打包完毕！", "确定");
                        }
                        else
                        {
                            EditorUtility.DisplayDialog("提示", "打包失败！", "确定");
                        }
                    }
                }
            };
            var oneKeyBtn = _root.Q<Button>("BtnOneKeyBuild");
            oneKeyBtn.clicked += async () =>
            {
                if (_buildRule != null)
                {
                    if (_nowPlatform == PlatformType.Nonsupport)
                    {
                        EditorUtility.DisplayDialog("提示", "当前平台不支持打包！", "确定");
                        return;
                    }
                    if (EditorUtility.DisplayDialog("提示", $"确定一键打AB包和安装包 - \"{_buildRule.desc}\"？", "确定", "取消"))
                    {
                        if (await AssetBundleBuildUtil.OneKeyBuild(_nowPlatform, _buildRule))
                        {
                            EditorUtility.DisplayDialog("提示", "打包完毕！", "确定");
                        }
                        else
                        {
                            EditorUtility.DisplayDialog("提示", "打包失败！", "确定");
                        }
                    }
                }
            };
            var saveBtn = _root.Q<Button>("BtnSave");
            saveBtn.clicked += () =>
            {
                SaveRule();
            };
            
            _isEditorUsed = _root.Q<Toggle>("IsEditorUsed");
            _sceneObj = _root.Q<ObjectField>("SceneObj");
            _txtDesc = _root.Q<TextField>("TxtDesc");
            _outputFolder = _root.Q<TextField>("OutputFolder");
            _btnOutputFolder = _root.Q<Button>("BtnOutputFolder");
            _copyFolder = _root.Q<TextField>("CopyFolder");
            _btnCopyFolder = _root.Q<Button>("BtnCopyFolder");
            _contrastFolder = _root.Q<TextField>("ContrastFolder");
            _btnContrastFolder = _root.Q<Button>("BtnContrastFolder");
            _incrementOutputFolder = _root.Q<TextField>("IncrementOutputFolder");
            _btnIncrementOutputFolder = _root.Q<Button>("BtnIncrementOutputFolder");
            _packageOutputFolder = _root.Q<TextField>("PackageOutputFolder");
            _btnPackageOutputFolder = _root.Q<Button>("BtnPackageOutputFolder");
            _mainVersion = _root.Q<TextField>("LabMainVersion");
            _subVersion = _root.Q<IntegerField>("LabSubVersion");
            _hotFixedVersion = _root.Q<IntegerField>("LabHotFixedVersion");
            _clearFolder = _root.Q<Toggle>("ClearFolder");
            _copyToStreamingAssets = _root.Q<Toggle>("CopyToStreamingAssets");
            _encryptABName = _root.Q<Toggle>("EncryptABName");
            _encryptAB = _root.Q<Toggle>("EncryptAB");
            _isDev = _root.Q<Toggle>("IsDev");
            _isSymbol = _root.Q<Toggle>("IsSymbol");
            _openDebugger = _root.Q<Toggle>("OpenDebugger");
            _logLevel = _root.Q<EnumFlagsField>("LogLevel");
            
            _isEditorUsed.RegisterValueChangedCallback(evt =>
            {
                if (evt.newValue)
                {
                    _buildSetting.editorUseBuildRule = _ruleFilePathList[_ruleFileSelectIndex];
                }
                else
                {
                    _buildSetting.editorUseBuildRule = String.Empty;
                }
                BuildSetting.SaveSetting(_buildSetting);
                _ruleFileList.Rebuild();
            });
            _sceneObj.RegisterValueChangedCallback(evt =>
            {
                _buildRule.sceneObj = evt.newValue;
            });
            _txtDesc.RegisterValueChangedCallback(evt =>
            {
                _buildRule.desc = evt.newValue;
            });
            _btnOutputFolder.clicked += () =>
            {
                var dir = EditorUtility.OpenFolderPanel("选择输出目录", Application.dataPath, String.Empty);
                if (!string.IsNullOrEmpty(dir))
                {
                    dir = EditorUtil.ConvertToRelativePath(dir);
                    _buildRule.outputPath = dir;
                    _outputFolder.value = dir;
                }
            };
            _btnCopyFolder.clicked += () =>
            {
                var dir = EditorUtility.OpenFolderPanel("选择拷贝目录", Application.dataPath, String.Empty);
                if (!string.IsNullOrEmpty(dir))
                {
                    dir = EditorUtil.ConvertToRelativePath(dir);
                    _buildRule.copyPath = dir;
                    _copyFolder.value = dir;
                }
            };
            _btnContrastFolder.clicked += () =>
            {
                var dir = EditorUtility.OpenFolderPanel("选择对照目录", Application.dataPath, String.Empty);
                if (!string.IsNullOrEmpty(dir))
                {
                    dir = EditorUtil.ConvertToRelativePath(dir);
                    _buildRule.contrastPath = dir;
                    _contrastFolder.value = dir;
                }
            };
            _btnIncrementOutputFolder.clicked += () =>
            {
                var dir = EditorUtility.OpenFolderPanel("选择增量包输出目录", Application.dataPath, String.Empty);
                if (!string.IsNullOrEmpty(dir))
                {
                    dir = EditorUtil.ConvertToRelativePath(dir);
                    _buildRule.incrementOutputPath = dir;
                    _incrementOutputFolder.value = dir;
                }
            };
            _btnPackageOutputFolder.clicked += () =>
            {
                var dir = EditorUtility.OpenFolderPanel("选择安装包输出目录", Application.dataPath, String.Empty);
                if (!string.IsNullOrEmpty(dir))
                {
                    dir = EditorUtil.ConvertToRelativePath(dir);
                    _buildRule.packageOutputPath = dir;
                    _packageOutputFolder.value = dir;
                }
            };
            _mainVersion.RegisterValueChangedCallback(evt =>
            {
                _buildRule.mainVersion = evt.newValue;
            });
            _subVersion.RegisterValueChangedCallback(evt =>
            {
                _buildRule.subVersion = evt.newValue;
            });
            _hotFixedVersion.RegisterValueChangedCallback(evt =>
            {
                _buildRule.hotFixedVersion = evt.newValue;
            });
            _clearFolder.RegisterValueChangedCallback(evt =>
            {
                _buildRule.isClearFolder = evt.newValue;
            });
            _copyToStreamingAssets.RegisterValueChangedCallback(evt =>
            {
                _buildRule.copyToStreamingAssets = evt.newValue;
            });
            _encryptABName.RegisterValueChangedCallback(evt =>
            {
                _buildRule.isEncryptABName = evt.newValue;
            });
            _encryptAB.RegisterValueChangedCallback(evt =>
            {
                _buildRule.isEncryptAB = evt.newValue;
            });
            _isDev.RegisterValueChangedCallback(evt =>
            {
                _buildRule.isDev = evt.newValue;
            });
            _isSymbol.RegisterValueChangedCallback(evt =>
            {
                _buildRule.isSymbol = evt.newValue;
            });
            _openDebugger.RegisterValueChangedCallback(evt =>
            {
                _buildRule.openDebugger = evt.newValue;
            });
            _logLevel.RegisterValueChangedCallback(evt =>
            {
                _buildRule.logLevel = (LogLevel) evt.newValue;
            });
            
            _togActive = _root.Q<Toggle>("TogActive");
            _descName = _root.Q<TextField>("DescName");
            _rulePackerType = _root.Q<EnumField>("RulePackerType");
            _togOverrideBundleName = _root.Q<Toggle>("TogOverrideBundleName");
            _customBundleName = _root.Q<TextField>("CustomBundleName");
            _assetSearchPath = _root.Q<TextField>("AssetSearchPath");
            _btnSelectFolder = _root.Q<Button>("BtnSelectFolder");
            _assetSearchOption = _root.Q<EnumField>("AssetSearchOption");
            _assetSearchPattern = _root.Q<TextField>("AssetSearchPattern");
            _assetSearchRegex = _root.Q<TextField>("AssetSearchRegex");
            _manifestInfoType = _root.Q<EnumField>("ManifestInfoType");
            
            _togActive.RegisterValueChangedCallback(evt =>
            {
                _buildRuleItem.active = evt.newValue;
                _ruleList.Rebuild();
            });
            _descName.RegisterValueChangedCallback(evt =>
            {
                _buildRuleItem.buildDesc = evt.newValue;
            });
            _rulePackerType.RegisterValueChangedCallback(evt =>
            {
                _buildRuleItem.buildType = (RulePackerType) evt.newValue;
            });
            _togOverrideBundleName.RegisterValueChangedCallback(evt =>
            {
                _buildRuleItem.isOverrideBundleName = evt.newValue;
            });
            _customBundleName.RegisterValueChangedCallback(evt =>
            {
                _buildRuleItem.overrideBundleName = evt.newValue;
            });
            _btnSelectFolder.clicked += () =>
            {
                var dir = EditorUtility.OpenFolderPanel("选择打包目录", Application.dataPath, String.Empty);
                if (!string.IsNullOrEmpty(dir))
                {
                    dir = EditorUtil.ConvertToRelativePath(dir);
                    _buildRuleItem.searchPath = dir;
                    _assetSearchPath.value = dir;
                    _ruleList.Rebuild();
                }
            };
            _assetSearchOption.RegisterValueChangedCallback(evt =>
            {
                _buildRuleItem.searchOption = (SearchOption) evt.newValue;
            });
            _assetSearchPattern.RegisterValueChangedCallback(evt =>
            {
                _buildRuleItem.searchPattern = evt.newValue;
            });
            _assetSearchRegex.RegisterValueChangedCallback(evt =>
            {
                _buildRuleItem.searchRegex = evt.newValue;
            });
            _manifestInfoType.RegisterValueChangedCallback(evt =>
            {
                _buildRuleItem.manifestWriteType = (ManifestWriteType) evt.newValue;
            });
            
            CheckPlatform();
            InitFileList();
            InitRuleList();
            
            RefreshRuleFile();
            RefreshRuleList();
        }

        private void OnProjectChanged()
        {
            RefreshRuleFile(true);
        }

        private void CheckPlatform()
        {
#if UNITY_ANDROID
			_nowPlatform = PlatformType.Android;
#elif UNITY_IOS
			_nowPlatform = PlatformType.iOS;
#elif UNITY_STANDALONE_WIN
            _nowPlatform = PlatformType.Windows;
#else
			_nowPlatform = PlatformType.Nonsupport;
#endif
            
            var label = _root.Q<Label>("LabBuildInfo");
            label.text = "当前打包平台：" + _nowPlatform;
            label.style.color = new StyleColor(_nowPlatform == PlatformType.Nonsupport ? Color.red : Color.green);
        }

        private void InitFileList()
        {
            _ruleFileList = _root.Q<ListView>("RuleFileList");
            _ruleFileList.SetViewController(new ListViewController());
            
            _ruleFileList.makeItem = () =>
            {
                VisualElement element = new VisualElement();
                element.style.flexDirection = FlexDirection.Row;

                var label = new Label();
                label.name = "labelRule";
                label.style.unityTextAlign = TextAnchor.MiddleLeft;
                label.style.flexGrow = 1f;
                label.style.height = 30f;
                label.style.paddingBottom = 2f;
                label.style.paddingTop = 2f;
                label.enableRichText = true;
                element.Add(label);

                return element;
            };
            _ruleFileList.bindItem = (element, index) =>
            {
                var ruleFilePath = _ruleFilePathList[index];
                var editorUse = _buildSetting.editorUseBuildRule == ruleFilePath;
                var label = element.Q<Label>("labelRule");
                label.text = Path.GetFileNameWithoutExtension(ruleFilePath) + (editorUse ? "[编辑器使用中]" : "");
                label.style.color = new StyleColor(editorUse ? Color.yellow : Color.green);
            };
            _ruleFileList.selectedIndicesChanged += indexList =>
            {
                foreach (var index in indexList)
                {
                    _ruleFileSelectIndex = index;
                    OnSelectFile();
                    break;
                }
            };

            var btnAdd = _root.Q<Button>("BtnFileAdd");
            btnAdd.clicked += () =>
            {
                EditorUtil.CreateDir(AssetBundleBuildUtil.RuleFileFolder);
                var path = EditorUtility.SaveFilePanelInProject("选择文件地址", "BuildRule", "asset", String.Empty, AssetBundleBuildUtil.RuleFileFolder);
                if (!string.IsNullOrEmpty(path))
                {
                    var buildRule = ScriptableObject.CreateInstance<BuildRule>();
                    AssetDatabase.CreateAsset(buildRule, path);
                    AssetDatabase.Refresh();
                    RefreshRuleFile();
                }
            };
            
            var btnRemove = _root.Q<Button>("BtnFileRemove");
            btnRemove.clicked += () =>
            {
                var index = _ruleFileSelectIndex;
                if (EditorUtility.DisplayDialog("提示", $"确定删除\"{_ruleFilePathList[index]}\"文件？", "确定", "取消"))
                {
                    AssetDatabase.DeleteAsset(_ruleFilePathList[index]);
                }
                RefreshRuleFile();
            };
        }

        private void RefreshRuleFile(bool onlyRefreshRuleFileList = false)
        {
            _ruleFileList.Clear();
            _ruleFilePathList.Clear();
            if (Directory.Exists(AssetBundleBuildUtil.RuleFileFolder))
            {
                var files = Directory.GetFiles(AssetBundleBuildUtil.RuleFileFolder, "*.asset", SearchOption.TopDirectoryOnly);
                foreach (var file in files)
                {
                    var rule = AssetDatabase.LoadAssetAtPath<BuildRule>(file);
                    if (rule != null)
                    {
                        _ruleFilePathList.Add(EditorUtil.FormatPath(file));
                    }
                }
            }
            _ruleFileList.viewController.itemsSource = _ruleFilePathList;
            _ruleFileList.Rebuild();

            if (onlyRefreshRuleFileList)
            {
                return;
            }
            
            if (_ruleFilePathList.Count == 0)
            {
                _ruleFileSelectIndex = -1;
            }
            else
            {
                if (_ruleFileSelectIndex < 0 || _ruleFileSelectIndex >= _ruleFilePathList.Count)
                {
                    _ruleFileSelectIndex = 0;
                }
            }

            _ruleFileList.selectedIndex = _ruleFileSelectIndex;
            OnSelectFile();
        }

        private void OnSelectFile()
        {
            var container = _root.Q<VisualElement>("RuleContentContainer");
            if (_ruleFileSelectIndex == -1)
            {
                container.visible = false;
                _rules = null;
                RefreshRuleList();
                return;
            }
            
            container.visible = true;

            SaveRule();
            
            var path = _ruleFilePathList[_ruleFileSelectIndex];
            _buildRule = AssetDatabase.LoadAssetAtPath<BuildRule>(path);
            _rules = _buildRule.rules;
            
            RefreshRuleInfo();
        }

        private void SaveRule()
        {
            if (_buildRule != null)
            {
                EditorUtility.SetDirty(_buildRule);
                AssetDatabase.SaveAssetIfDirty(_buildRule);
            }
        }

        private void RefreshRuleInfo()
        {
            _isEditorUsed.value = _buildSetting.editorUseBuildRule == _ruleFilePathList[_ruleFileSelectIndex];
            _sceneObj.value = _buildRule.sceneObj;
            _txtDesc.value = _buildRule.desc;
            _outputFolder.value = _buildRule.outputPath;
            _copyFolder.value = _buildRule.copyPath;
            _contrastFolder.value = _buildRule.contrastPath;
            _incrementOutputFolder.value = _buildRule.incrementOutputPath;
            _packageOutputFolder.value = _buildRule.packageOutputPath;
            _mainVersion.value = _buildRule.mainVersion;
            _subVersion.value = _buildRule.subVersion;
            _hotFixedVersion.value = _buildRule.hotFixedVersion;
            _clearFolder.value = _buildRule.isClearFolder;
            _copyToStreamingAssets.value = _buildRule.copyToStreamingAssets;
            _encryptABName.value = _buildRule.isEncryptABName;
            _encryptAB.value = _buildRule.isEncryptAB;
            _isDev.value = _buildRule.isDev;
            _isSymbol.value = _buildRule.isSymbol;
            _openDebugger.value = _buildRule.openDebugger;
            _logLevel.value = _buildRule.logLevel;
            
            RefreshRuleList();
        }

        private void InitRuleList()
        {
            _ruleList = _root.Q<ListView>("RuleList");
            _ruleList.SetViewController(new ListViewController());
            
            _ruleList.makeItem = () =>
            {
                VisualElement element = new VisualElement();
                element.style.flexDirection = FlexDirection.Row;

                var label = new Label();
                label.name = "labelBoundName";
                label.style.unityTextAlign = TextAnchor.MiddleLeft;
                label.style.flexGrow = 1f;
                label.style.height = 30f;
                label.style.paddingBottom = 2f;
                label.style.paddingTop = 2f;
                label.enableRichText = true;
                element.Add(label);

                return element;
            };
            _ruleList.bindItem = (element, index) =>
            {
                var item = _rules[index];
                
                var name = "未设置打包目录";
                if (!string.IsNullOrEmpty(item.searchPath))
                {
                    name = Path.GetFileNameWithoutExtension(item.searchPath);
                }
                
                var label = element.Q<Label>("labelBoundName");
                label.text = name;
                label.style.color = new StyleColor(item.active ? Color.green : Color.gray);
            };
            _ruleList.selectedIndicesChanged += indexList =>
            {
                foreach (var index in indexList)
                {
                    _rulesSelectIndex = index;
                    RefreshRuleList();
                    break;
                }
            };
            
            var btnAdd = _root.Q<Button>("BtnRuleAdd");
            btnAdd.clicked += () =>
            {
                _rules.Add(new BuildRuleItem());
                RefreshRuleList();
            };
            
            var btnRemove = _root.Q<Button>("BtnRuleRemove");
            btnRemove.clicked += () =>
            {
                var index = _rulesSelectIndex;
                var name = "未设置打包目录";
                if (!string.IsNullOrEmpty(_rules[index].searchPath))
                {
                    name = Path.GetFileNameWithoutExtension(_rules[index].searchPath);
                }
                if (EditorUtility.DisplayDialog("提示", $"确定删除\"{name}\"文件？", "确定", "取消"))
                {
                    _rules.RemoveAt(index);
                    RefreshRuleList();
                }
            };
            
            var disableAllBtn = _root.Q<Button>("BtnDisableAll");
            disableAllBtn.clicked += () =>
            {
                foreach (var ruleItem in _rules)
                {
                    ruleItem.active = false;
                }
                _ruleList.Rebuild();
                RefreshRuleItemInfo();
            };
            
            var enableAllBtn = _root.Q<Button>("BtnEnableAll");
            enableAllBtn.clicked += () =>
            {
                foreach (var ruleItem in _rules)
                {
                    ruleItem.active = true;
                }
                _ruleList.Rebuild();
                RefreshRuleItemInfo();
            };
        }

        private void RefreshRuleList()
        {
            _ruleList.viewController.itemsSource = _rules;
            _ruleList.Rebuild();
            
            if (_rules == null || _rules.Count == 0)
            {
                _rulesSelectIndex = -1;
                _buildRuleItem = null;
            }
            else
            {
                if (_rulesSelectIndex < 0 || _rulesSelectIndex >= _buildRule.rules.Count)
                {
                    _rulesSelectIndex = 0;
                }
                _buildRuleItem = _rules[_rulesSelectIndex];
            }

            _ruleList.selectedIndex = _rulesSelectIndex;
            RefreshRuleItemInfo();
        }

        private void RefreshRuleItemInfo()
        {
            var container = _root.Q<VisualElement>("BuildRuleContainer");
            if (_buildRuleItem == null)
            {
                container.visible = false;
                return;
            }
            container.visible = true;
            
            _togActive.value = _buildRuleItem.active;
            _descName.value = _buildRuleItem.buildDesc;
            _rulePackerType.value = _buildRuleItem.buildType;
            _togOverrideBundleName.value = _buildRuleItem.isOverrideBundleName;
            _customBundleName.value = _buildRuleItem.overrideBundleName;
            _assetSearchPath.value = _buildRuleItem.searchPath;
            _assetSearchOption.value = _buildRuleItem.searchOption;
            _assetSearchPattern.value = _buildRuleItem.searchPattern;
            _assetSearchRegex.value = _buildRuleItem.searchRegex;
            _manifestInfoType.value = _buildRuleItem.manifestWriteType;
        }

        private void OnDestroy()
        {
            EditorApplication.projectChanged -= OnProjectChanged;
            BuildSetting.SaveSetting(_buildSetting);
            SaveRule();
        }
    }
}
